﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using DevelopTool.Bll.DbBlls;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.CommonControls;
using DevelopTool.Forms.DbForms.FunctionForms;
using DevelopTool.Forms.DbForms.PackageForms;
using DevelopTool.Forms.DbForms.ProcedureForms;
using DevelopTool.Forms.DbForms.SequenceForms;
using DevelopTool.Forms.DbForms.SynonymForms;
using DevelopTool.Forms.DbForms.TableForms;
using DevelopTool.Forms.DbForms.TriggerForms;
using DevelopTool.Forms.DbForms.ViewForms;
using DevelopTool.Forms.LodingForm;
using DevelopTool.Model;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.DbModels;

namespace DevelopTool.Forms.DbForms.ReferenceForms
{
    /// <summary>
    /// Oracle对象依赖关系信息窗体
    /// </summary>
    public partial class OracleReferenceForm : BaseUIPage
    {
        /// <summary>
        /// 连接信息
        /// </summary>
        private DbInfo dbInfo;

        /// <summary>
        /// Oracle操作类
        /// </summary>
        private OracleDbBll oracleDbBll;

        /// <summary>
        /// 资源类型
        /// </summary>
        private string queryTypeName;

        /// <summary>
        /// 查询实例
        /// </summary>
        private string queryDataBase;

        /// <summary>
        /// 查询资源名
        /// </summary>
        private string queryObjectName;


        /// <summary>
        /// DataGridView列信息集合 依赖
        /// </summary>
        private List<DataGridColumn> dataGridColumns1 = new List<DataGridColumn>()
              {
                { new DataGridColumn("OWNER"){ColumnText="实例",Width=100,ColumnType=ColumnType.Hide}},
                { new DataGridColumn("NAME"){ColumnText="对象名",Width=100,ColumnType=ColumnType.Hide}},
                { new DataGridColumn("TYPE"){ColumnText="类型",Width=100,ColumnType = ColumnType.Hide}},

                { new DataGridColumn("REFERENCED_NAME"){ColumnText="依赖对象名",Width=0}},
                { new DataGridColumn("REFERENCED_TYPE"){ColumnText="对象类型",Width=-1}},
                { new DataGridColumn("REFERENCED_OWNER"){ColumnText="对象实例",Width=-1}},

                { new DataGridColumn("REFERENCED_LINK_NAME"){ColumnText="实例",Width=100,ColumnType = ColumnType.Hide}},
                { new DataGridColumn("DEPENDENCY_TYPE"){ColumnText="对象名",Width=100,ColumnType = ColumnType.Hide}},

              };


        /// <summary>
        /// DataGridView列信息集合 被依赖
        /// </summary>
        private List<DataGridColumn> dataGridColumns2 = new List<DataGridColumn>()
              {
                { new DataGridColumn("NAME"){ColumnText="被依赖对象名",Width=0}},
                { new DataGridColumn("TYPE"){ColumnText="对象类型",Width=-1}},
                { new DataGridColumn("OWNER"){ColumnText="对象实例",Width=-1}},

                { new DataGridColumn("REFERENCED_NAME"){ColumnText="对象名",Width=100,ColumnType = ColumnType.Hide}},
                { new DataGridColumn("REFERENCED_TYPE"){ColumnText="类型",Width=100,ColumnType = ColumnType.Hide}},
                { new DataGridColumn("REFERENCED_OWNER"){ColumnText="实例",Width=100,ColumnType = ColumnType.Hide}},

                { new DataGridColumn("REFERENCED_LINK_NAME"){ColumnText="实例",Width=100,ColumnType = ColumnType.Hide}},
                { new DataGridColumn("DEPENDENCY_TYPE"){ColumnText="对象名",Width=100,ColumnType = ColumnType.Hide}},

              };


        public OracleReferenceForm(DbInfo dbInfo,string queryDataBase,string queryObjectName,string queryTypeName)
        {
            InitializeComponent();

            this.dbInfo = dbInfo.CloneObj();
            this.queryDataBase = queryDataBase;
            this.queryObjectName = queryObjectName;
            this.queryTypeName = queryTypeName;
        }

        private void OracleReferenceForm_Load(object sender,EventArgs e)
        {
            oracleDbBll = new OracleDbBll(dbInfo);

            InitDate();
        }


        private void InitDate()
        {
            DataTable dataTable1 = new DataTable();
            DataTable dataTable2 = new DataTable();

            LoadingHelper.ShowLoading("获取依赖关系数据",() =>
               {
                   //查询依赖对象信息
                   dataTable1 = oracleDbBll.GetReferences(queryDataBase,queryObjectName,queryTypeName,1);
                   dataTable2 = oracleDbBll.GetReferences(queryDataBase,queryObjectName,queryTypeName,2);
               });

            this.dataDisplayControl1.BindData(dataTable1,this.dataGridColumns1,"REFERENCED_TYPE ASC");
            this.dataDisplayControl2.BindData(dataTable2,this.dataGridColumns2,"TYPE ASC");

        }


        private void dataDisplayControl1_DataGridViewCellClick(DataGridViewEventArgs dataGridViewEventArgs)
        {
            if(dataGridViewEventArgs.MouseButton == MouseButtons.Left)//判断点的是不是左键
            {
                if(dataGridViewEventArgs.Row.Cells["REFERENCED_NAME"].Value != null && !dataGridViewEventArgs.Row.Cells["REFERENCED_NAME"].Value.ToString().IsEmpty())
                {
                    string name = dataGridViewEventArgs.Row.Cells["REFERENCED_NAME"].Value.ToString();
                    string owner = dataGridViewEventArgs.Row.Cells["REFERENCED_OWNER"].Value.ToString();
                    string type = dataGridViewEventArgs.Row.Cells["REFERENCED_TYPE"].Value.ToString();

                    ShowRigthObjectInfo(name,owner,type);
                }
            }
        }


        private void dataDisplayControl2_DataGridViewCellClick(DataGridViewEventArgs dataGridViewEventArgs)
        {
            if(dataGridViewEventArgs.MouseButton == MouseButtons.Left)//判断点的是不是左键
            {
                if(dataGridViewEventArgs.Row.Cells["NAME"].Value != null && !dataGridViewEventArgs.Row.Cells["NAME"].Value.ToString().IsEmpty())
                {
                    string name = dataGridViewEventArgs.Row.Cells["NAME"].Value.ToString();
                    string owner = dataGridViewEventArgs.Row.Cells["OWNER"].Value.ToString();
                    string type = dataGridViewEventArgs.Row.Cells["TYPE"].Value.ToString();

                    ShowRigthObjectInfo(name,owner,type);
                }

            }
        }


        /// <summary>
        /// 显示右侧数据库资源详情
        /// </summary>
        /// <param name="name"></param>
        /// <param name="owner"></param>
        /// <param name="type"></param>
        private void ShowRigthObjectInfo(string name,string owner,string type)
        {
            DbInfo db = this.dbInfo.CloneObj();
            Form form = null;

            if(type == "表")
            {
                form = new OracleTableForm(db,owner,name);
            }
            else if(type == "视图")
            {
                OracleViewForm oracleViewForm = new OracleViewForm(db,owner,name);
                oracleViewForm.FixedFirstRowColumnName = "TEXT";
                form = oracleViewForm;
            }
            else if(type == "函数")
            {
                OracleFunctionForm oracleFunctionForm = new OracleFunctionForm(db,owner,name);
                oracleFunctionForm.FixedFirstRowColumnName = "TEXT";
                form = oracleFunctionForm;
            }
            else if(type == "存储过程")
            {
                OracleProcedureForm oracleProcedureForm = new OracleProcedureForm(db,owner,name);
                oracleProcedureForm.FixedFirstRowColumnName = "TEXT";
                form = oracleProcedureForm;
            }
            else if(type == "触发器")
            {
                OracleTriggerForm oracleTriggerForm = new OracleTriggerForm(db,owner,name);
                oracleTriggerForm.FixedFirstRowColumnName = "TRIGGER_BODY";
                form = oracleTriggerForm;
            }
            else if(type == "同义词")
            {
                OracleSynonymForm oracleSynonymForm = new OracleSynonymForm(db,owner,name);
                oracleSynonymForm.FixedFirstRowColumnName = "SYNONYM_NAME";
                form = oracleSynonymForm;
            }
            else if(type == "包" || type == "包体")
            {
                OraclePackageForm oraclePackageForm = new OraclePackageForm(db,owner,name);
                oraclePackageForm.FixedFirstRowColumnName = "PACKAGEBODY_TEXT";
                form = oraclePackageForm;
            }
            else if(type == "序列")
            {
                OracleSequenceForm oracleSequenceForm = new OracleSequenceForm(db,owner,name);
                oracleSequenceForm.FixedFirstRowColumnName = "SEQUENCE_NAME";
                form = oracleSequenceForm;
            }

            if(form != null)
                TabControlHelper.AddFormToControl(uiSplitContainer1.Panel2,form);
        }


        private void dataDisplayControl1_DataReLoad(object sender,EventArgs e)
        {
            InitDate();
        }

        private void dataDisplayControl2_DataReLoad(object sender,EventArgs e)
        {
            InitDate();
        }

    }
}
